<template>
  <div class="main">
    <el-row :gutter="20" justify="center" type="flex">
        <el-col :span="18">
            <el-form ref="newsForm" :model="news"
                :rules="rules"  label-width="80px"
                :inline="false" size="mini" status-icon>
                <el-form-item label="主题" prop="ntid">
                    <el-select placeholder="请选择" v-model="news.ntid" clearable filterable style="width:200px">
                        <el-option v-for="topic in topicList"
                            :key="topic.tid"
                            :label="topic.tname"
                            :value="topic.tid">
                        </el-option>
                    </el-select>                   
                </el-form-item>
                
                <el-form-item label="标题" prop="ntitle">
                    <el-input style="width:200px" v-model="news.ntitle"></el-input>
                </el-form-item>

                <el-form-item label="作者" prop="nauthor">
                    <el-input style="width:200px" v-model="news.nauthor"></el-input>
                </el-form-item>

                <el-form-item label="摘要" prop="nsummary">
                    <el-input v-model="news.nsummary"></el-input>
                </el-form-item>

                <el-form-item label="内容" prop="ncontent">
                    <el-input type="textarea" v-model="news.ncontent" :rows="5"  placeholder="" 
                    :maxlength="-1" :show-word-limit="false" >
                    </el-input>
                </el-form-item>
                
                <el-form-item>
                    <el-button type="primary" @click="submitForm">修改</el-button>
                    <el-button @click="back()">取消</el-button>
                </el-form-item>
            </el-form>
            
        </el-col>
    </el-row>
    
  </div>
</template>

<script>
export default {
    name:'NewsEdit',
    data(){
        return {
            topicList:[],
            news:{
                ntid:'',
                ntitle:'',
                nauthor:'',
                nsummary:'',
                ncontent:''
            },
            rules:{
                ntid:[
                    {
                        type: "number",
                        required: true,
                        trigger: "change",
                        message: "请选择新闻类别",
                    }
                ],
                ntitle:[
                    {
                        type:'string',
                        required:true,
                        trigger:'blur',
                        message:'请输入标题'
                    }
                ],
                nauthor:[
                    {
                        type:'string',
                        required:true,
                        trigger:'blur',
                        message:'请输入作者'
                    }
                ],
                nsummary:[
                    {
                        type:'string',
                        required:true,
                        trigger:'blur',
                        message:'请输入摘要'
                    }
                ],
                ncontent:[
                    {
                        type:'string',
                        required:true,
                        trigger:'blur',
                        message:'请输入内容'
                    }
                ]
            }
        } 
    },
    created(){
        this.allTopic()
        this.getNews()
    },
    methods:{
        allTopic(){
            this.$axios({
                url:'/api/allTopic'
            }).then(rs=>{
                this.topicList=rs.data.data
            })
        },
        getNews(){
            this.$axios({
                url:'/api/showNews',
                params:{
                    nid:this.$route.query.nid
                }
            }).then(rs=>{
                this.news = rs.data.data
            })
        },
        back(){
            this.$router.back()
        },
        submitForm(){
            this.$refs.newsForm.validate(flag=>{
                if(flag){
                    this.$axios({
                        url:'/api/editNews',
                        method:'post',
                        data:{
                            ...this.news
                        }
                    }).then(rs=>{
                        if(rs.data.data){
                            this.$router.replace({name:'NewsManager'},()=>{})
                            this.$message.success('修改成功')
                        }else{
                            this.$message.error('修改失败')
                        }
                    })
                }
            })
        }
    }
}
</script>

<style>

</style>